define(function(require, exports, module) {
	"use strict";
	require("mui");
	require("jquery");
	require("../newOrder/dateInput.js");
	var muiTips = require("muiTips"); //封装的弹框
	var abpUse = require("../abp.js");
	var abp = abpUse.abpstart;
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//钉钉返回清除缓存
	var dingTalkNav = require('dd_config');
	dingTalkNav.RightNavBar(false, "更多", "default"); //钉钉右侧按钮隐藏
	$(function() {
		//上传图片
		mui("body").on("tap", ".add-new-img", function() {
			dingTalkNav.uploadImage(uploadIMG);
		});
		//   
		function uploadIMG(result) {
			var imgSRC;
			var srcList = result;
			//			$("body").append(JSON.stringify(result));
			for(var i = 0; i < srcList.length; i++) {
				imgSRC = srcList[i];
				$(".add-new-img").before('<img class="goodsIMG" src="' + imgSRC + '" alt="" />');
			}
		}

		//图片点击浏览
		mui("body").on("tap", ".goodsIMG", function() {
			var urls = [];
			var img = $(".goodsIMG");
			var imgURL;
			for(var i = 0; i < img.length; i++) {
				imgURL = $(".goodsIMG").eq(i).attr("src");
				urls.push(imgURL);
			}
			var current = $(this).attr("src");
			dingTalkNav.previewImage(urls, current);
		});

		//扫码
		mui("body").on("tap", "#scan-icon", function() {
			dingTalkNav.ScanCode(ScanCode);
		});

		function ScanCode(data) {
			$("#product-bar").val(data.text);
		}

		//钉钉返回清除缓存
		if(sessionStorage.getItem("goodsHome")) {
			//ios
			dingTalkNav.LeftNavBar(clearSessionDD);
			//ad
			dingTalkNav.leftAndroid(clearSessionDD);
		}

		function clearSession() {
			sessionStorage.removeItem("sortCatalogId");
			sessionStorage.removeItem("sortGoodsType");
			sessionStorage.removeItem("sortName");
			sessionStorage.removeItem("productUpdate");
		}

		function clearSessionDD() {
			sessionStorage.removeItem("sortCatalogId");
			sessionStorage.removeItem("sortGoodsType");
			sessionStorage.removeItem("sortName");
			sessionStorage.removeItem("productUpdate");
			document.location.href = "../../src/chioceProduct/chioceProductList.html?dd_nav_bgcolor=FFFF943E";
		}

		mui("body").on("tap", "header>button", function() {
			clearSession();
		});
		var goodsType = $("#goods-type").text();
		if(sessionStorage.sortCatalogId && sessionStorage.sortName && sessionStorage.sortGoodsType) {
			$("#goods-type").text(sessionStorage.sortName);
			$("#goods-type").attr("data-id", sessionStorage.sortCatalogId);
			$("#goods-type").attr("data-type", sessionStorage.sortGoodsType);
			//			getSerialNo();	
		} else {
			if(!goodsType) {
				$("#goods-type").text("(必选)");
			} else {
				$("#goods-type").text(goodsType);
				//				getSerialNo();	
			}
		}

		getSerialNo();
		if(!sessionStorage.getItem("data-stockQuery")) {
			sortList();
		}
		//货品分类Id
		function sortList() { //产品分类信息列表获取函数
			var option = {};
			var dataModel = {
				"id": 0
			};
			abp.services.app.infoGoodsCatalog.getList(dataModel, option).
			done(function(data) {
				var goodsType = $("#goods-type").text();
				for(var i = 0; i < data.length; i++) {
					if(goodsType == data[i].catalogName) {
						$("#goods-type").attr("data-id", data[i].id);
						$("#goods-type").attr("data-type", data[i].goodsType);
						getSerialNo();
						//	break;
					}
				}
			}).fail(function(data) {

			});
		}
		//获取货品编号
		function getSerialNo() {
			var goodsCatalogId = $("#goods-type").attr("data-id");
			var option = {};
			var dataModel = {
				"goodsCatalogId": goodsCatalogId
			};
			abp.services.app.infoGoods.getSerialNo(dataModel, option)
				.done(function(data) {
					//console.log(data);
					$("#goods-num").text(data.serialNo);
				})
				.fail(function(data) {
					muiTips.alertAutoma(' ', data.message, 2, "default");
				});
		}

		function alertTshow(i) { //输入框获取焦点 提示窗隐藏
			$(".alertT").eq(i).stop().animate({
				"top": "70px"
			}, 400, function() {
				$(this).delay(1000).fadeOut(500, function() {
					$(this).css("top", "-55px");
					$(this).show();
				});
			});
		}

		function ADDinvalid(i) {
			$(".testI").eq(i).addClass("invalid");
			$(".testI").eq(i).attr("placeholder", "不能为空");
		}

		function REMOVEinvalid(i) {
			$(".testI").eq(i).removeClass("invalid");
		}

		function subGoods() {
			var catalogId = Number($("#goods-type").attr("data-id"));
			//alert(catalogId);return;
			var goodsSerial = $("#goods-num").text().trim();
			var goodsName = $("#goods-name").val().trim();
			var goodsRule = $("#goods-rule").val().trim();
			var unit = $("#goods-unit").val().trim();
			var goodsType = Number($("#goods-type").attr("data-type"));
			var costPrice = $("#goods-cost").val().trim();
			var sellMinPrice = $("#goods-price").val().trim();
			var goodsBid = $("#goods-bid").val().trim();
			var barCode = $("#product-bar").val().trim();
			var notes = $(".many-text").val();
			//获取图片信息
			var infoGoodsImg = [];
			var imgs = $(".goodsIMG");

			for(var i = 0; i < imgs.length; i++) {
				var imgSrc = $(".goodsIMG").eq(i).attr("src");
				var imgSrcObj = {
					"imgPath": imgSrc, //(string): 图片路径,
					"imgTitle": imgSrc, //(string): 图片名,
					"id": 0 //(integer, optional): Id
				};
				infoGoodsImg.push(imgSrcObj);
			}
			//invalid  testI
			if(!goodsSerial) {
				$("#goods-num").addClass("red");
			} else {
				$("#goods-num").removeClass("red");
			}
			if(!goodsName) {
				ADDinvalid(0);
			} else {
				REMOVEinvalid(0);
			}
			if(!goodsType) {
				$("#goods-type").addClass("red");
			} else {
				$("#goods-type").removeClass("red");
			}
			if(!goodsRule) {
				ADDinvalid(1);
			} else {
				REMOVEinvalid(1);
			}
			if(!unit) {
				ADDinvalid(2);
			} else {
				REMOVEinvalid(2);
			}

			if(!goodsSerial) {
				alertTshow(0);
				return;
			}
			if(!goodsName) {
				alertTshow(1);
				return;
			}
			if(!goodsType) {
				alertTshow(2);
				return;
			}
			if(!goodsRule) {
				alertTshow(3);
				return;
			}
			if(!unit) {
				alertTshow(4);
				return;
			}

			var option = {};
			var dataModel = {
				"catalogId": catalogId, //0分类 ,
				"goodsSerial": goodsSerial, //str货品编号 ,
				"goodsName": goodsName, //str货品名称 ,
				"barCode": barCode, //str条形码 ,
				"unit": unit, //str
				"whId": 0, //0仓库id ,
				"notes": notes, //str备注 ,
				"goodsType": goodsType, //0
				"costPrice": costPrice, //0参考成本 ,
				"buyMaxPrice": goodsBid, //0最大采购价 ,
				"sellMinPrice": sellMinPrice, //0最低销售价 ,
				"specValue": goodsRule, //(string, optional): 规格 ,
				"buyCycle": 1, //(number, optional): 采购周期 ,
				"dailyProduceNumber": 2, //(number, optional): 日产量 ,
				"infoGoodsImg": infoGoodsImg,
				"infoGoodsSpecValue": [],
				"id": 0 //0
			};
			abp.services.app.infoGoods.create(dataModel, option)
				.done(function(data) {
					//console.log(data);
					muiTips.alertAutoma(' ', "保存成功", 2, clearSessionDD);
				})
				.fail(function(data) {
					muiTips.alertAutoma(' ', data.message, 2, "default");
				});
		}
		mui("body").on("tap", "#submit-btn", function() {
			subGoods();
		});

		mui("body").on("tap", "#back", function() { //返回传参显示编辑和删除按钮
			sessionStorage.flags = "show";
		});

	});

});